# Copyright (c) 2020 Kosaki Mezumona
# This cmake script is distributed under the MIT License.
# See LICENSE for more details.
cmake_minimum_required(VERSION 3.2)
set(quickjs_url @quickjs_url@)
set(quickjs_sha1 @quickjs_sha1@)
set(quickjs_extras_url @quickjs_extras_url@)
set(quickjs_extras_sha1 @quickjs_extras_sha1@)
set(quickjs_extract_dir @quickjs_extract_dir@)
set(quickjs_sources @quickjs_sources@)

set(must_extract FALSE)
set(quickjs_tar @CMAKE_CURRENT_BINARY_DIR@/quickjs.tar.xz)
set(quickjs_extras_tar @CMAKE_CURRENT_BINARY_DIR@/quickjs-extra.tar.xz)

if(NOT EXISTS ${quickjs_tar})
	file(DOWNLOAD
		${quickjs_url}
		${quickjs_tar}
		EXPECTED_HASH SHA1=${quickjs_sha1}
	)
	set(must_extract TRUE)
endif()

if(NOT EXISTS ${quickjs_extras_tar})
	file(DOWNLOAD
		${quickjs_extras_url}
		${quickjs_extras_tar}
		EXPECTED_HASH SHA1=${quickjs_extras_sha1}
	)
	set(must_extract TRUE)
endif()

foreach(source IN LISTS quickjs_sources)
	if (NOT EXISTS ${source})
		set(must_extract TRUE)
		break()
	endif()
endforeach()


if (must_extract)
	execute_process(COMMAND ${CMAKE_COMMAND} -E remove_directory ${quickjs_extract_dir})
	execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${quickjs_extract_dir})
	execute_process(
		COMMAND ${CMAKE_COMMAND} -E tar xJ ${quickjs_tar}
		WORKING_DIRECTORY ${quickjs_extract_dir}
	)
	execute_process(
		COMMAND ${CMAKE_COMMAND} -E tar xJ ${quickjs_extras_tar}
		WORKING_DIRECTORY ${quickjs_extract_dir}
	)
endif()
